package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.base.service.impl.BaseServiceImpl;
import com.example.common.utils.ApiResult;
import com.example.dao.SuppliesMapper;
import com.example.dao.SuppliesPropMapper;
import com.example.dao.SuppliesRealinfoWarningMapper;
import com.example.dao.SuppliesTypeMapper;
import com.example.model.entity.*;
import com.example.model.query.SuppliesQuery;
import com.example.model.query.SuppliesRealinfoWarningQuery;
import com.example.service.SuppliesRealinfoWarningService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SuppliesRealinfoWarningServiceImpl extends BaseServiceImpl<SuppliesRealinfoWarningMapper, SuppliesRealinfoWarning> implements SuppliesRealinfoWarningService {

	@Autowired
	private SuppliesRealinfoWarningMapper realinfoWarningMapper;

	@Autowired
	private SuppliesMapper suppliesMapper;

	@Autowired
	private SuppliesPropMapper suppliesPropMapper;

	@Autowired
	private SuppliesTypeMapper suppliesTypeMapper;

	private static final Logger logger = LoggerFactory.getLogger(SuppliesRealinfoWarningServiceImpl.class);
	
	/**
	 * 序列
	 */
	@Override
	public Long seqAlways() {
		return realinfoWarningMapper.seqAlways();
	}

	@Override
	public ApiResult<SuppliesRealinfoWarningQuery> pageUserVoByQuery(SuppliesRealinfoWarningQuery query) throws Exception{
		query.setOptimizeCountSql(false);
		Long current = query.getCurrent();
		query.setCurrent(1L);
//		QueryWrapper<SuppliesRealinfoWarning> queryWrapper = new QueryWrapper<>();
//		queryWrapper.lambda().eq(SuppliesRealinfoWarning::getIsDanger,1L);
		Integer total = realinfoWarningMapper.countByQuery(query);
		query.setTotal(total);
		query.setCurrent(current);
		realinfoWarningMapper.pageInfoByQuery(query);
		List list = query.getRecords();
		for (int i = 0; i < list.size(); i++) {
			SuppliesRealinfoWarningQuery waringQuery = (SuppliesRealinfoWarningQuery) list.get(i);
			Supplies supplies = suppliesMapper.selectById(waringQuery.getSuppliesId());
			if(supplies != null){
				waringQuery.setSuppliesName(supplies.getSuppliesName());
				waringQuery.setSuppliesNum(supplies.getSuppliesNum());
				SuppliesType type = suppliesTypeMapper.selectById(supplies.getTypeId());
				if(type != null) {
					waringQuery.setSuppliesTypeName(type.getTypeName());
				}
				//物资状态数据处理
				QueryWrapper<SuppliesProp> suppliesPropQueryWrapper = new QueryWrapper<>();
				suppliesPropQueryWrapper.lambda().eq(SuppliesProp::getIsValid,1L)
						.eq(SuppliesProp::getTypeId,supplies.getTypeId());
				List<SuppliesProp> propList = suppliesPropMapper.selectList(suppliesPropQueryWrapper);
				String str ="";
				if(propList.size()>0 && StringUtils.isNotBlank(waringQuery.getRealInfo())){
					JSONObject json =JSONObject.parseObject(waringQuery.getRealInfo());
					for(SuppliesProp ob : propList){
						String propData = json.getString(ob.getPropLabel());
						if(StringUtils.isNotBlank(propData)){
							String linRead = ob.getPropName()+":"+propData+"\n";
							str = str + linRead;
						}
					}
				}
				waringQuery.setRealInfoString(str);
			}
		}
		return new ApiResult<>(query);
	}



}
